package com.znyj.cloud.chain.tcphandler;


import com.znyj.cloud.chain.util.CheckUtil;
import com.znyj.cloud.device.constant.SysConstant;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.apache.log4j.Logger;


/**
 * Created by python on 15/6/5.
 */

public class ClientIdleTimeoutHandler extends ChannelDuplexHandler {
    private static final Logger logger = Logger.getLogger(ClientIdleTimeoutHandler.class);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState e = ((IdleStateEvent) evt).state();
            if (e == IdleState.READER_IDLE) {

                logger.error(CheckUtil.monitorReport(SysConstant.ERROR_CODE.READ_IDLE, "IP:" + ctx.channel().remoteAddress().toString(), 0));
                if (ctx.channel().isActive()) {
                    ctx.fireChannelInactive();
                    ctx.close();
                }
            }
        }
    }
}
